package mis510.homefinders;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Viewreq extends ListActivity implements OnClickListener{
	String useremail_req = null;
	ArrayList<String> prev_req = new ArrayList<String>();
	ArrayList<String> ec = new ArrayList<String>();
	DBUserAdapter dbUser = new DBUserAdapter(Viewreq.this);
	ListView req_list;
	ArrayAdapter<String> arrayAdapter;
	Button bk_btn;
	TextView updated_date;
	TextView hint;
	Intent nextActivity;
	final Calendar c = Calendar.getInstance();
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewreq);

        Intent intent = this.getParent().getIntent();
		Bundle bundle = intent.getExtras();
		useremail_req = bundle.getString("useremail");
		System.out.println("user email req: "+useremail_req);
		//req_list = (ListView)findViewById(R.id.list);
		bk_btn = (Button)findViewById(R.id.bk_btn);
		bk_btn.setOnClickListener(this);
		updated_date = (TextView)findViewById(R.id.updated_date);
		hint = (TextView)findViewById(R.id.hint);
		int month = c.get(Calendar.MONTH)+1;
		updated_date.setText("Updated: " +month+"-"+c.get(Calendar.DAY_OF_MONTH)+"-"+c.get(Calendar.YEAR));
		dbUser.open();
		prev_req = dbUser.Reqretrieve(useremail_req);
		System.out.println(prev_req.get(0));
		if(prev_req.get(0).equals("Currently no one matches your requirements, please come back and check later.") || prev_req.get(0).equals("You haven't submitted any roommate request yet."))
		{
			hint.setText(prev_req.get(0));
			prev_req.remove(0);
		}
		else
			hint.setText("Interested in someone ? Click him !");
		for (int i = 0; i < prev_req.size(); i++) {
		     System.out.println(prev_req.get(i));
		}
		arrayAdapter = new ArrayAdapter<String>(this, R.layout.list_item, prev_req);
		
		
		req_list = getListView();
		req_list.setAdapter(arrayAdapter); 
		req_list.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				String selectedperson =(String) (req_list.getItemAtPosition(position));
				System.out.println("selected: "+selectedperson);
				System.out.println("position: "+position);
				ec = dbUser.Emailsending(position,useremail_req);
				if(ec!=null)
				{
					//Toast.makeText(Viewreq.this,"Your information has been sent", Toast.LENGTH_LONG).show();
					new SendEmail().execute(ec.get(0),ec.get(1),ec.get(2),ec.get(3),ec.get(4),ec.get(5),ec.get(6),ec.get(7),ec.get(8),ec.get(9),ec.get(10),ec.get(11),ec.get(12),ec.get(13));
					
				}
//				else
//					Toast.makeText(Viewreq.this,"Email failure", Toast.LENGTH_LONG).show();
			}
		});
	}
	
	
	@Override
	public void onClick(View v) {
		switch( v.getId() )
		{
		case R.id.bk_btn:
		{
			nextActivity = new Intent(Viewreq.this, SearchProperty.class);
        	startActivity(nextActivity);
        	break;
		}
		}
	}
	
	public class SendEmail extends AsyncTask<String, Void, Boolean>
	{

		@Override
		protected Boolean doInBackground(String... econtent) {
			String[] econtent_asyn = econtent;
			sendmail send = new sendmail();
			String name = null;
			send.SendEmail(econtent_asyn[0], econtent_asyn[1], econtent_asyn[2], econtent_asyn[3], econtent_asyn[4], econtent_asyn[5], econtent_asyn[6], econtent_asyn[7], econtent_asyn[8], econtent_asyn[9], econtent_asyn[10], econtent_asyn[11], econtent_asyn[12], econtent_asyn[13]);
			Boolean sent = true;
			return sent;
		}
		
		protected void onPostExecute(Boolean sent) {
			if(sent)
				Toast.makeText(Viewreq.this,"Your information has been sent", Toast.LENGTH_LONG).show();
			else
				Toast.makeText(Viewreq.this,"Email failure", Toast.LENGTH_LONG).show();
        }
		
	}

}
